package com.ruoyi.mall.controller;

import java.util.List;

import com.ruoyi.mall.domain.MallMemberUser;
import com.ruoyi.mall.domain.MallShopExpressCode;
import com.ruoyi.mall.service.IMallMemberUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mall.domain.MallMemberUserDistributor;
import com.ruoyi.mall.service.IMallMemberUserDistributorService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 分销商列Controller
 *
 * @author ruoyi
 * @date 2022-03-15
 */
@RestController
@RequestMapping("/mall/mallMemberUserDistributor")
public class MallMemberUserDistributorController extends BaseController
{
    @Autowired
    private IMallMemberUserDistributorService mallMemberUserDistributorService;
    @Autowired
    private IMallMemberUserService mallMemberUserService;

    /**
     * 查询分销商列列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallMemberUserDistributor:list')")
    @GetMapping("/list")
    public TableDataInfo list(MallMemberUserDistributor mallMemberUserDistributor)
    {
        startPage();
        List<MallMemberUserDistributor> list = mallMemberUserDistributorService.selectMallMemberUserDistributorList(mallMemberUserDistributor);
        return getDataTable(list);
    }

    /**
     * 导出分销商列列表
     */
    @PreAuthorize("@ss.hasPermi('mall:mallMemberUserDistributor:export')")
    @Log(title = "分销商列", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(MallMemberUserDistributor mallMemberUserDistributor)
    {
        List<MallMemberUserDistributor> list = mallMemberUserDistributorService.selectMallMemberUserDistributorList(mallMemberUserDistributor);
        ExcelUtil<MallMemberUserDistributor> util = new ExcelUtil<MallMemberUserDistributor>(MallMemberUserDistributor.class);
        return util.exportExcel(list, "分销商列数据");
    }

    /**
     * 获取分销商列详细信息
     */
    @PreAuthorize("@ss.hasPermi('mall:mallMemberUserDistributor:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(mallMemberUserDistributorService.selectMallMemberUserDistributorById(id));
    }

    /**
     * 新增分销商列
     */
    @PreAuthorize("@ss.hasPermi('mall:mallMemberUserDistributor:add')")
    @Log(title = "分销商列", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallMemberUserDistributor mallMemberUserDistributor)
    {
        return toAjax(mallMemberUserDistributorService.insertMallMemberUserDistributor(mallMemberUserDistributor));
    }

    /**
     * 修改分销商列
     */
    @PreAuthorize("@ss.hasPermi('mall:mallMemberUserDistributor:edit')")
    @Log(title = "分销商列", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallMemberUserDistributor mallMemberUserDistributor)
    {
        // 查推荐人信息
        MallMemberUser introUser = mallMemberUserService.selectMallMemberUserById(mallMemberUserDistributor.getIntroUser());
        // 自己的信息
        MallMemberUser user = mallMemberUserService.selectMallMemberUserById(mallMemberUserDistributor.getId());
        if (mallMemberUserDistributor.getIntroUser() == 0) {
            return AjaxResult.error("请选择推荐人");
        }
        if (introUser == null) {
            return AjaxResult.error("推荐人信息异常");
        }
        if (introUser.getId().equals(user.getId())) {
            return AjaxResult.error("不可绑定自己");
        }
        if (user.getId().equals(introUser.getIntroUser())) {
            return AjaxResult.error("不可相互绑定");
        }
        MallMemberUser memberUser = new MallMemberUser();
        memberUser.setId(user.getId());
        memberUser.setIntroUser(introUser.getId());
        memberUser.setIntroUserInfo(introUser.getName()+"--"+introUser.getPhone());
//        mallMemberUserService.updateUserIntroUserInfo(memberUser);
//        return toAjax(mallMemberUserDistributorService.insertMallMemberUserDistributor(mallMemberUserDistributor));
        return toAjax(mallMemberUserService.updateUserIntroUserInfo(memberUser));
    }

    /**
     * 删除分销商列
     */
    @PreAuthorize("@ss.hasPermi('mall:mallMemberUserDistributor:remove')")
    @Log(title = "分销商列", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mallMemberUserDistributorService.deleteMallMemberUserDistributorByIds(ids));
    }

    /**
     * 查询分销商列表
     */
    @GetMapping("/DistributorList")
    public TableDataInfo list(MallMemberUser mallMemberUser)
    {
        startPage();
        List<MallMemberUser> list = mallMemberUserService.selectDistributorList(mallMemberUser);
        return getDataTable(list);
    }

    /**
     * 查询用户列表
     */
    @GetMapping("/getUserInfoList")
    public AjaxResult getUserInfoList(MallMemberUser mallMemberUser)
    {
        List<MallMemberUser> list = mallMemberUserService.selectDistributorList(mallMemberUser);
        return AjaxResult.success(list);
    }
}
